/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evolve.webbot.file;
import  java.util.Date;
import  java.text.SimpleDateFormat;
import  java.util.ArrayList;
import  java.util.StringTokenizer;
import  java.io.IOException;
import  java.io.BufferedReader;
import  java.io.FileReader;
import  java.io.FileWriter;
import  java.io.PrintWriter;
import  com.evolve.webbot.bean.Comer;
/**
 *
 * @author DarthAE
 */
public class TxtComerMex 
{
    private String filePath;

    public TxtComerMex(){}
    
    public TxtComerMex(String filePath)
    {
        this.filePath=filePath;
    } 
    
    public ArrayList<Comer>  readComerMexTxtFile()
    {
      ArrayList<Comer> comermexRecordList= new ArrayList<Comer>();  
      boolean inicioCant = false;  
            
      try {
             BufferedReader br = new BufferedReader(new FileReader(this.filePath));
             String linea = br.readLine();

             while (linea != null) 
             {

               if(linea.contains("TOTALES"))
               {
                   inicioCant = false;
               }    
               
               if(inicioCant)
               {  
                 comermexRecordList.add(getValues(linea.trim()));
               }               
               
               if(linea.contains("Selec."))
               {
                    inicioCant = true; 
               }   
               
               linea = br.readLine();
             }
             
           } catch(IOException e){}
      
      return comermexRecordList;
    }
    
    public Comer getValues(String linea)
    {
        Comer comermex = new Comer();
        StringTokenizer tokens = null;
        String  value     = null;
        String  invLinea  = null;
        int     i         = 0;
        int     k         = 0;
        
        tokens = new StringTokenizer(linea);
        
        while(tokens.hasMoreTokens()&& k<1)
        {
            value=tokens.nextToken().trim();
            k++;
        }
        
        comermex.setArticulo(value);
        linea = (linea.replaceAll(value,"")).trim();
        
        invLinea = invierteCadena(linea);
        
        tokens = new StringTokenizer(invLinea);
        
        while(tokens.hasMoreTokens()&& i<7)
        {
           value=tokens.nextToken().trim();
           value=invierteCadena(value);
           /*System.out.println(value);*/
           
           switch(i)
           {
               case 0: comermex.setMesAct(value);
                       break;
               case 1: comermex.setMesSeis(value);
                       break;
               case 2: comermex.setMesCinco(value);
                       break;
               case 3: comermex.setMesCuatro(value);
                       break;
               case 4: comermex.setMesTres(value);
                       break;
               case 5: comermex.setMesDos(value);
                       break;
               case 6: comermex.setMesUno(value);
                       break;
               default:System.out.println("-");
                       break;  
           }
           
           i++;
        }
        
        comermex.setDescripcion((linea.substring(0,linea.indexOf(comermex.getMesUno()))).trim());
        return comermex;
    }
    
    public String cleanValue(String value)
    {
        String cleanValue = null;
        
        if(value!=null)
        {
             for(int i=0;i<127;i++)
             {   
               if(i!=46 && (i<=47 || i>=58))
               {
                 char n = (char)i;
                 value = value.replace(n,' ');
                 value = value.replaceAll(" ","");
               }                 
             }             
             cleanValue = value;       
        }
        
        return cleanValue;
    }
    
    public void writeComerMexDataFile(ArrayList<Comer> sorianaRecordList,int tipoHeader)
    {
        try {
               FileWriter  outFile = new FileWriter(this.filePath.replaceAll("vyc","data"));
               PrintWriter out     = new PrintWriter(outFile);
              
               out.println(generaHeader(tipoHeader));
               for(int i=0;i<sorianaRecordList.size();i++)
               {
                 out.println(sorianaRecordList.get(i).toString());
               }
              
               out.close();
               
            } catch (IOException e){e.printStackTrace();}
    }
    
    public String invierteCadena(String linea)
    {
        String lineaInversa = "";
        
        for (int x=linea.length()-1;x>=0;x--)
        {
            lineaInversa = lineaInversa + linea.charAt(x);
        }		
        
        return lineaInversa;
    }
    
    public String generaHeader(int tipoHeader)
    {
        String header   = "";
        String actMonth ="";
        int    numMonth =  0;
        Date   date = new Date();
        SimpleDateFormat sdf00 = new SimpleDateFormat("MM");
        
        numMonth = Integer.parseInt(sdf00.format(date));
        
        actMonth = getMonth(numMonth);
        
        for(int i =(numMonth - 6);i<numMonth;i++)
        {
            //System.out.println(getMonth(i));
            header=header+"|"+getMonth(i);
        }
        
        header=header+"|"+actMonth;
        
        if(tipoHeader==0||tipoHeader==1||
           tipoHeader==4||tipoHeader==5)
        {
            header="articulo|descripcion"+header;
        }
        else
        if(tipoHeader==2||tipoHeader==3||
           tipoHeader==6||tipoHeader==7)
        {
            header="clave|sucursal"+header;
        }
        
        return header;
    }
    
    public String getMonth(int numMonth)
    {
        String month="";
        
        switch(numMonth)
        {
            case 1:month="enero";break;
            case 2:month="febrero";break;
            case 3:month="marzo";break;
            case 4:month="abril";break;
            case 5:month="mayo";break;
            case 6:month="junio";break;
            case 7:month="julio";break;
            case 8:month="agosto";break;
            case 9:month="septiembre";break;
            case 10:month="octubre";break;
            case 11:month="noviembre";break;
            case 12:month="diciembre";break;
            default:break;
        }
        
        return month;
    }
    
//    public static void main(String args[])
//    {
////        ArrayList<Comer> comerMexRecordList = new ArrayList<Comer>();
//        TxtComerMex txtComerMex                = new TxtComerMex("C:\\test\\comermex-vyc-20120904-21-00-00-00.txt");
//        System.out.println(txtComerMex.generaHeader(0));
//        System.out.println(txtComerMex.generaHeader(2));
////        
////        comerMexRecordList = txtComerMex.readComerMexTxtFile();
////        txtComerMex.writeComerMexDataFile(comerMexRecordList);
//    }    
}
